package com.xinrui;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class freeMarkerDemo {
    public static void main(String[] args) throws IOException, TemplateException {
//        1.创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
//        2.设置模板所在目录
        configuration.setDirectoryForTemplateLoading(new File("D:\\ftl"));
//        3.设置字符集
        configuration.setDefaultEncoding("utf-8");
//        4.加载模板
//        Template template = configuration.getTemplate("test.ftl");
        Template template = configuration.getTemplate("freeMarker.ftl");
//        5.创建数据模型，使用map
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("name","张三");
//        map.put("message","欢迎您的到来！");
        HashMap hashMap = new HashMap();
        hashMap.put("mobile","16680430443");
        hashMap.put("address","湖南永州");
        hashMap.put("flog",true);
//        集合
        List goodsList = new ArrayList();
//         集合里面存放map
//        map1
        Map hashMap1 = new HashMap();
        hashMap1.put("name","张三");
        hashMap1.put("age",16);
//        map2
        Map hashMap2 = new HashMap();
        hashMap2.put("name","李四");
        hashMap2.put("age",17);
//        map3
        Map hashMap3 = new HashMap();
        hashMap3.put("name","王五");
        hashMap3.put("age",18);
//        将map都添加到集合中
        goodsList.add(hashMap1);
        goodsList.add(hashMap2);
        goodsList.add(hashMap3);
//        再将集合添加到hashMap中
        hashMap.put("goodsList",goodsList);
//        6.创建输出文件路径对象
        FileWriter writer = new FileWriter("D:\\ftl\\test.html");
//        7.将数据填充到模板
        template.process(hashMap,writer);
//        关闭对象
        writer.close();
    }
}
